package example1;

//1. 两数之和

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class LeetCode1 {
    public static void main(String[] args) {
        int[] arr = new int[]{2,7,11,15};
        System.out.println(Arrays.toString(new LeetCode1().twoSum(arr,9)));
    }

    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map = new HashMap<>();
        int[] arr = new int[2];
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(target - nums[i])){
                int j = map.get(target - nums[i]);
                arr[0] = j;
                arr[1] = i;
                break;
            }
            map.put(nums[i],i);
        }
        return arr;
    }
}
